package com.ita.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ita.dto.ReplyDto;
import com.ita.pojo.Assess;
import com.ita.service.AssessService;
import com.ita.util.Constant;
import com.ita.util.JsonResult;
import com.ita.vo.AssessVo;
import com.ita.vo.PagingResult;

@RestController
@CrossOrigin
public class AssessController {

	@Autowired(required = true)
	private AssessService aService;

	@RequestMapping(value = "sec/assess/reply", method = RequestMethod.PATCH, consumes = "application/json")
	public JsonResult replyAssess(@RequestBody ReplyDto rd) {

		String reply=rd.getReply();
		String id=rd.getId();
		JsonResult result = new JsonResult();
		if (reply.equals("") || reply.length() == 0) {
			result.setCode(Constant.FAIL);// 404
			result.setMsg("Not allow empty reply");
			return result;
		}
		Assess assess = null;
		try {
			assess = aService.replyToAssess(reply, id);
		} catch (Exception e) {
			result.setCode(Constant.FAIL);
			result.setMsg("Server error!");
			return result;
		}
		if (assess == null) {
			result.setCode(Constant.FAIL);
		} else {
			result.setCode(Constant.SUCCESS);
//			result.setData(assess);
			result.setMsg("Reply success");
		}
		return result;
	}


	@RequestMapping(value = "sec/assess/list", method = RequestMethod.POST, consumes = "application/json")
	public JsonResult getListByPaging(@RequestBody PagingResult pr) {

		JsonResult result = new JsonResult();
		String mid = (String) pr.getData();
		int max = pr.getMax();
		int begin = max*(pr.getPage()-1);

		List<Assess> list = aService.findAllByMidAndPaging(mid, max, begin);
		List<AssessVo> vList = new ArrayList<AssessVo>();

		for (Assess assess : list) {
			AssessVo assessVo = new AssessVo();
			assessVo.setId(assess.getId());
			assessVo.setAssertTime(assess.getAssess_time());
			assessVo.setMname(assess.getmUser().getName());
			assessVo.setMsg(assess.getMsg());
			assessVo.setReply(assess.getReply());
			assessVo.setReplyTime(assess.getReply_time());
			assessVo.setScore(assess.getScore());
			assessVo.setCname(assess.getcName());
			vList.add(assessVo);
		}
		result.setCode(Constant.SUCCESS);
		result.setData(vList);
		return result;

	}
	
	@RequestMapping(value = "sec/assess/replylist", method = RequestMethod.POST, consumes = "application/json")
	public JsonResult getReplyListByPaging(@RequestBody PagingResult pr) {
		
		JsonResult result = new JsonResult();
		String mid = (String) pr.getData();
		int max = pr.getMax();
		int begin = max*(pr.getPage()-1);
		
		List<Assess> list = aService.findAllReplyByMidAndPaging(mid, max, begin);
		List<AssessVo> vList = new ArrayList<AssessVo>();
		
		for (Assess assess : list) {
			AssessVo assessVo = new AssessVo();
			assessVo.setId(assess.getId());
			assessVo.setAssertTime(assess.getAssess_time());
			assessVo.setMname(assess.getmUser().getName());
			assessVo.setMsg(assess.getMsg());
			assessVo.setReply(assess.getReply());
			assessVo.setReplyTime(assess.getReply_time());
			assessVo.setScore(assess.getScore());
			assessVo.setCname(assess.getcName());
			vList.add(assessVo);
		}
		result.setCode(Constant.SUCCESS);
		result.setData(vList);
		return result;
		
	}
	
	
	@RequestMapping(value = "sec/assess/noreplylist", method = RequestMethod.POST, consumes = "application/json")
	public JsonResult getNoReplylistListByPaging(@RequestBody PagingResult pr) {
		
		JsonResult result = new JsonResult();
		String mid = (String) pr.getData();
		int max = pr.getMax();
		int begin = max*(pr.getPage()-1);
		
		List<Assess> list = aService.findAllNoReplyByMidAndPaging(mid, max, begin);
		List<AssessVo> vList = new ArrayList<AssessVo>();
		
		for (Assess assess : list) {
			AssessVo assessVo = new AssessVo();
			assessVo.setId(assess.getId());
			assessVo.setAssertTime(assess.getAssess_time());
			assessVo.setMname(assess.getmUser().getName());
			assessVo.setMsg(assess.getMsg());
			assessVo.setReply(assess.getReply());
			assessVo.setReplyTime(assess.getReply_time());
			assessVo.setScore(assess.getScore());
			assessVo.setCname(assess.getcName());
			vList.add(assessVo);
		}
		result.setCode(Constant.SUCCESS);
		result.setData(vList);
		return result;
		
	}
	
	@RequestMapping(value = "sec/assess/init", method = RequestMethod.POST, consumes = "application/json")
	public JsonResult initPagingSize(@RequestBody PagingResult pr) {
		
		JsonResult result = new JsonResult();
		String mid = (String) pr.getData();//5
		int max=pr.getMax();
		
		List<Integer> list = aService.initSize(mid,max);
		result.setCode(Constant.SUCCESS);
		result.setData(list);
		return result;
		
	}

}
